// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SocialAuthResponseController.java

package com.nervytech.tutorcruise.spring.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.spring.bean.SocialAuthTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.nervytech.tutorcruise.db.dao.model.User;
import com.nervytech.tutorcruise.db.enums.UserTypeEnum;
import com.nervytech.tutorcruise.db.service.interfaces.IUserService;
import com.nervytech.tutorcruise.spring.security.auth.SessionUser;

@Controller
@RequestMapping("/usr/socialauth")
public class SocialAuthResponseController {

	private static final Logger logger = LoggerFactory.getLogger("");
	private SocialAuthTemplate socialAuthTemplate;
	
	@Autowired
	private IUserService userService;
	@Autowired
	private UserDetailsService userDetailsService;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private SaltSource saltSource;
	protected AuthenticationManager authenticationManager;

	public String registerUser(Map model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		AuthProvider provider;
		SocialAuthManager manager = socialAuthTemplate.getSocialAuthManager();
		provider = manager.getCurrentAuthProvider();
		String providerId = provider.getProviderId();
		System.out.println((new StringBuilder(
				"Provider Id is ====>>>>>>>>>>>>>>> ")).append(providerId)
				.toString());
		boolean isUserExist;
		Profile socialProfile = provider.getUserProfile();
		System.out.println((new StringBuilder("FFFFFFFFFFFFF ")).append(
				socialProfile.toString()).toString());
		isUserExist = userService.checkIfUserExist(socialProfile.getEmail());
		User usr = userService.getUserByEmail(socialProfile.getEmail());
		if (isUserExist)
			return "redirect:/usr/profile";
		/*
		 * break MISSING_BLOCK_LABEL_130; Exception e; e; e.printStackTrace();
		 */
		return "";
	}

	private boolean autoLogin(User usr, String fullName,
			HttpServletRequest request) {
		try {
			Collection authorities = new ArrayList();
			authorities.add(new SimpleGrantedAuthority(UserTypeEnum.DEFAULT
					.toString()));
			SessionUser springUser = new SessionUser(usr.getEmail(),
					usr.getPassword(), authorities, true, fullName,
					usr.getUserId());
			org.springframework.security.core.Authentication authentication = new UsernamePasswordAuthenticationToken(
					springUser, springUser.getPassword(),
					springUser.getAuthorities());
			SecurityContextHolder.getContext()
					.setAuthentication(authentication);
			request.getSession().setAttribute("SPRING_SECURITY_CONTEXT",
					SecurityContextHolder.getContext());
		} catch (Exception e) {
			e.printStackTrace();
			SecurityContextHolder.getContext().setAuthentication(null);
			return false;
		}
		return true;
	}

}
